package com.guyaning.deyson_ec.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatTextView;
import android.util.Patterns;
import android.view.View;

import com.blankj.utilcode.util.RegexUtils;
import com.guyaning.deyson_core.core.delegates.DeysonDelegate;
import com.guyaning.deyson_ec.ec.R;
import com.guyaning.deyson_ec.ec.R2;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/3.
 */

public class SignUpDelegate extends DeysonDelegate {

    @BindView(R2.id.edit_sign_up_name)
    TextInputEditText mName = null;
    @BindView(R2.id.edit_sign_up_email)
    TextInputEditText mEmail = null;
    @BindView(R2.id.edit_sign_up_phone)
    TextInputEditText mPhone = null;
    @BindView(R2.id.edit_sign_up_password)
    TextInputEditText mPassword = null;
    @BindView(R2.id.edit_sign_up_re_password)
    TextInputEditText mRePassword = null;
    @BindView(R2.id.btn_sign_up)
    AppCompatButton btnSignUp = null;
    @BindView(R2.id.tv_link_sign_in)
    AppCompatTextView tvLinkSignIn = null;

    private   ISignListener iSignListener = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if(activity instanceof  ISignListener){
            iSignListener = (ISignListener) activity;
        }
    }

    @OnClick(R2.id.tv_link_sign_in)
    public void LinkSignIn(){
        //跳转到登录界面
       start(new SignInDelegate(),SINGLETASK);
    }

    @OnClick(R2.id.btn_sign_up)
    public void SignUp() {

//        if(checkForm()){
//            RestClient.builder()
//                      .url("sign_up")
//                      .loader(getContext())
//                      .params("","")
//                      .success(new ISuccess() {
//                          @Override
//                          public void onSuccess(String response) {
//                             SignHandler.onSignUp(response,iSignListener);
//
//                          }
//                      })
//                     .build()
//                     .post();
//        }
    }

    private boolean checkForm(){

        String name = mName.getText().toString().trim();
        String email = mEmail.getText().toString().trim();
        String phone = mPhone.getText().toString().trim();
        String password = mPassword.getText().toString().trim();
        String rePassword = mRePassword.getText().toString().trim();

        boolean isPass = true;

        if(name.isEmpty()){
            mName.setError("请输入姓名");
            isPass = false;
        }else {
            mName.setError(null);
        }

        if(email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            mEmail.setError("邮箱格式错误");
            isPass = false;
        }else {
            mEmail.setError(null);
        }

        if(phone.isEmpty()|| !RegexUtils.isMobileSimple(phone)){
           mPhone.setError("请输入正确的手机号");
           isPass = false;
        }else {
            mPhone.setError(null);
        }

        if(password.isEmpty() || password.length()<6){
            mPassword.setError("密码不能少于六位");
            isPass = false;
        }else {
            mPassword.setError(null);
        }

        if(rePassword.isEmpty() || rePassword.length()<6 ||!(rePassword.equals(password))){
           mRePassword.setError("请重新确认密码");
           isPass = false;
        }else {
            mRePassword.setError(null);
        }

        return isPass;

    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {

    }

}
